package ${packageName}.${subPackageName};

import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.framework.common.model.Pagination;
import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.framework.common.model.SortQuery;
import com.bingxue.edu.framework.mybatis.query.QueryBuilder;
import com.bingxue.edu.framework.security.ApiResource;
import ${packageName}.model.entity.${EntityName};
import ${packageName}.model.query.${EntityName}Query;
import ${packageName}.model.req.${EntityName}Req;
import ${packageName}.model.resp.${EntityName}DetailResp;
import ${packageName}.model.resp.${EntityName}Resp;
import ${packageName}.service.${EntityName}Service;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * ${businessName}接口
 *
 * @author ${author}
 * @since ${date}
 */
@Tag(name = "${businessName}接口")
@RestController
@RequestMapping("/${moduleName}/${entityName}")
@RequiredArgsConstructor
public class ${className} {

    private final ${EntityName}Service ${entityName}Service;

    @Operation(description = "查询列表")
    @ApiResource("${moduleName}:${entityName}:read")
    @GetMapping("/list")
    public Response<List<${EntityName}Resp>> list(${EntityName}Query query, SortQuery sortQuery) {
        QueryWrapper queryWrapper = QueryBuilder.build(query, sortQuery);
        List<${EntityName}Resp> list = ${entityName}Service.listAs(queryWrapper, ${EntityName}Resp.class);
        return Response.success(list);
    }

    @Operation(description = "分页查询列表")
    @ApiResource("${moduleName}:${entityName}:read")
    @GetMapping("/page")
    public Response<Pagination<${EntityName}Resp>> page(${EntityName}Query query, @Validated PageQuery pageQuery) {
        QueryWrapper queryWrapper = QueryBuilder.build(query, pageQuery);
        Page<${EntityName}Resp> page = ${entityName}Service.pageAs(pageQuery.toPage(), queryWrapper, ${EntityName}Resp.class);
        return Response.success(Pagination.of(page));
    }

    @Operation(description = "查询详情")
    @ApiResource("${moduleName}:${entityName}:read")
    @GetMapping("/detail/{id}")
    public Response<${EntityName}DetailResp> detail(@PathVariable String id) {
        ${EntityName}DetailResp detail = ${entityName}Service.getMapper().selectOneWithRelationsByIdAs(id, ${EntityName}DetailResp.class);
        return Response.success(detail);
    }

    @Operation(description="创建数据")
    @ApiResource("${moduleName}:${entityName}:write")
    @PostMapping("/create")
    public Response<Void> create(@RequestBody @Validated ${EntityName}Req req) {
        ${EntityName} ${entityName} = ${entityName}Service.copy(req);
        ${entityName}Service.save(${entityName});
        return Response.success();
    }

    @Operation(description="更新数据")
    @ApiResource("${moduleName}:${entityName}:write")
    @PostMapping("/update/{id}")
    public Response<Void> update(@RequestBody @Validated ${EntityName}Req req, @PathVariable String id) {
        ${EntityName} ${entityName} = ${entityName}Service.copyById(req, id);
        ${entityName}Service.updateById(${entityName});
        return Response.success();
    }

    @Operation(description="删除数据")
    @ApiResource("${moduleName}:${entityName}:write")
    @PostMapping("/delete/{ids}")
    public Response<Void> delete(@PathVariable List<String> ids) {
        ${entityName}Service.removeByIds(ids);
        return Response.success();
    }

}
